package com.bdmc.web.xxjob;

import cn.hutool.json.JSONUtil;
import com.bdmc.common.utils.MapDistance;
import com.bdmc.common.utils.StringUtils;
import com.bdmc.device.domain.vo.DevGpsRealtimeVo;
import com.bdmc.device.service.IDevGpsRealtimeService;
import com.vortex.tool.httpclient.HttpMethodName;
import com.vortex.tool.httpclient.Protocol;
import com.vortex.tool.httpclient.VtxHttpClient;
import com.vortex.tool.httpclient.credential.VtxCredential;
import com.vortex.tool.httpclient.credential.VtxDefaultCredential;
import com.vortex.tool.httpclient.request.VtxHttpRequest;
import com.vortex.tool.httpclient.sign.VtxV1Signer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class MyTask {

    @Autowired
    IDevGpsRealtimeService gpsRealtimeService;

    @Scheduled(fixedDelay = 1000 * 120)
    public void GpsUploadJobHandler() {
        log.info("洪山大数据环卫上传工牌数据到市平台.");
        //离线的不传
        List<DevGpsRealtimeVo> gpsList = gpsRealtimeService.queryUploadRealtime();
        log.info("实时在线数据条数："+gpsList.size());
        List<UploadDataModel> uploadWatchModelList = new ArrayList<>();
        int count = 0;
        for (DevGpsRealtimeVo gpsRealtimeVo : gpsList) {
            count++;
            UploadDataModel model = new UploadDataModel();
            if(StringUtils.isNotEmpty(gpsRealtimeVo.getWgs84Lat())){
                model.setLat(gpsRealtimeVo.getWgs84Lat());
                model.setLng(gpsRealtimeVo.getWgs84Lng());
            }else{
                double[] toWgs84 = MapDistance.gcj02_To_Wgs84(Double.parseDouble(gpsRealtimeVo.getLng()), Double.parseDouble(gpsRealtimeVo.getLat()));
                model.setLat(String.valueOf(toWgs84[1]));
                model.setLng(String.valueOf(toWgs84[0]));
            }
            model.setDeviceId("STAFF"+gpsRealtimeVo.getDevicePhoneNum());
            model.setLocationMode("1");
            long ts = gpsRealtimeVo.getDataTime().toInstant( ZoneOffset.ofHours(8)).toEpochMilli();
            model.setGpsTime(String.valueOf(ts));

            uploadWatchModelList.add(model);
            if(count%10==0 || count == gpsList.size()){
                VtxHttpRequest request = new VtxHttpRequest();
                try {
                    String str = JSONUtil.toJsonStr(uploadWatchModelList);
                    log.info(str);
                    request.withHttpMethod(HttpMethodName.POST)
//                            .withUri(new URI("http://58.48.74.42:28082/device/data/acs/staff/receive"))
                            .withUri(new URI("http://111.172.134.14:8082/device/data/acs/staff/receive"))
                            // 2024-08-15  更换工牌市平台转发
                            .withContent(str.getBytes())
                            .withConnectTimeout(100000);
                } catch (URISyntaxException e) {
                    e.printStackTrace();
                }
                VtxCredential credential = new VtxDefaultCredential("28379803610513411", "cf840b27376c11eea2dc3448edf7d204");
                VtxHttpClient client = new VtxHttpClient(credential, new VtxV1Signer(), Protocol.HTTP);
                String result = client.execute(request);
                System.out.println(result);
                uploadWatchModelList.clear();
            }
        }
        log.info("洪山大数据环卫上传工牌数据到市平台 完成");
    }
}
